<template>
  <div class="login-container">
    <forgetPass :dialogVisible="dialogVisible" v-if="dialogVisible" @close="close" />
    <div class="login-main">
      <div class="main-left common-main">
        <div class="left-top">
          <!--          <img src="../../assets/login_images/logo.png" alt="" />-->
          <!--          <span>北京实验小学</span>-->
        </div>
        <div class="left-main"></div>
      </div>
      <div class="main-right common-main">
        <div class="login-right-form">
          <div class="login-form">
            <div class="welcomeCn">您好！</div>
            <div class="welcomeCn">欢迎使用智慧体育平台！</div>
            <div class="form">
              <el-form
                :model="loginForm"
                :rules="loginRules"
                ref="loginForm"
                class="demo-ruleForm"
              >
                <el-form-item prop="username">
                  <el-input
                    clearable
                    prefix-icon="el-icon-user"
                    placeholder="请输入账号"
                    v-model="loginForm.username"
                    autocomplete="off"
                    style="width: 370px"
                  ></el-input>
                </el-form-item>
                <el-form-item prop="password">
                  <el-input
                    clearable
                    prefix-icon="el-icon-lock"
                    placeholder="请输入密码"
                    type="password"
                    v-model="loginForm.password"
                    autocomplete="off"
                    style="width: 370px"
                  ></el-input>
                </el-form-item>
                <el-form-item prop="captcha" style="height: 44px">
                  <div
                    style="
                      display: flex;
                      width: 370px;
                      height: 44px;
                      justify-content: center;
                    "
                  >
                    <div style="width: 400px; height: 0px">
                      <el-input
                        clearable
                        style="width: 270px"
                        prefix-icon="el-icon-lock"
                        placeholder="请输入计算结果"
                        v-model="loginForm.captcha"
                        autocomplete="off"
                      ></el-input>
                    </div>
                    <div style="width: 200px; margin-left: 10%">
                      <img
                        @click="changeImg"
                        :src="imgUrl"
                        style="height: 90%; width: 100px; margin-left: -35px"
                        alt=""
                      />
                    </div>
                  </div>
                </el-form-item>
                <div class="pass-box">
                  <div class="box-center">
                    <el-checkbox v-model="checked">下次自动登录</el-checkbox>
                    <el-link @click="dialogVisible = true" type="primary" style="margin-right: 230px">忘记密码？</el-link>

                  </div>

                </div>
                <el-form-item>
                  <el-button :loading="loading" @click="handleLogin"
                    >登录</el-button
                  >
                </el-form-item>
              </el-form>
            </div>
          </div>
        </div>
      </div>
    </div>
    <div class="text">
      Copyright 2022 by 智慧体育平台1.0 all rights reserved [浙IPC备15040643号-9]
    </div>
  </div>
</template>

<script>
import forgetPass from "@/views/login/components/forgetPass"
import { randomImg } from "@/api/login";
import randomNum from "@/utils/random";
import encryption from "@/utils/encryption";
import Decrypt from '@/utils/Decrypt'
import { verifyMobile } from "@/utils/validate";
import Crypto from "crypto-js";
export default {
  name: "Login",
  data() {
    const validateUsername = (rule, value, callback) => {
      if (!value) {
        callback(new Error("请输入您的账户名"));
      } else if (!verifyMobile(value)) {
        callback(new Error("账户名不合规"));
      } else {
        callback();
      }
    };
    const validatePassword = (rule, value, callback) => {
      if (!value) {
        callback(new Error("请输入您的密码"));
      } else {
        callback();
      }
    };
    const validateCode = (rule, value, callback) => {
      if (!value) {
        callback(new Error("请输入计算结果"));
      } else {
        callback();
      }
    };
    return {
      loginForm: {
        username: "",
        password: "",
        captcha: "",
        checkKey: "",
      },
      dialogVisible:false,
      loginRules: {
        username: [
          { required: true, trigger: "blur", validator: validateUsername },
        ],
        password: [
          { required: true, trigger: "blur", validator: validatePassword },
        ],
        captcha: [{ required: true, trigger: "blur", validator: validateCode }],
      },
      loading: false,
      passwordType: "",
      redirect: undefined,
      checked: false,
      imgUrl: "",
      id: "",
    };
  },

  components:{
    forgetPass
  },
  created() {
    this.changeImg();
    this.pass();
  },
  watch: {
    $route: {
      handler: function (route) {
        this.redirect = route.query && route.query.redirect;
      },
      immediate: true,
    },
  },
  methods: {
    close(){
        this.dialogVisible = false
    },
    changeImg() {
      this.loginForm.checkKey = randomNum();
      randomImg(this.loginForm.checkKey).then((response) => {
        if (response.code === 0) {
          this.imgUrl = response.result.pic;
          this.id = response.result.id;
        }
      });
    },
    showPwd() {
      if (this.passwordType === "password") {
        this.passwordType = "";
      } else {
        this.passwordType = "password";
      }
      this.$nextTick(() => {
        this.$refs.password.focus();
      });
    },
    pass() {
      if (localStorage.getItem("zhty-accountNumber")) {
        this.loginForm.username = JSON.parse(Decrypt(localStorage.getItem("zhty-accountNumber"))).username;
        this.loginForm.password = JSON.parse(Decrypt(localStorage.getItem("zhty-accountNumber"))).password;
        this.checked = true;
      }
    },
    handleLogin() {
      this.$refs.loginForm.validate((valid) => {
        if (valid) {
          this.loading = true;
          this.$store
            .dispatch("user/login", {
              ...this.loginForm,
              username: encryption(this.loginForm.username),
              password: encryption(this.loginForm.password),
              id: this.id,
            })
            .then((res) => {
              this.loading = false;
              if (res.code === 200) {
                if (res.result.userInfo.schoolStatus == "0" || res.result.userInfo.userStatus == 1) {
                  this.$alert("此账号已禁用，请联系管理员", "", {
                    confirmButtonText: "关闭",
                  });
                  return;
                }
                if (res.result.userInfo.roleId == 0) {
                  this.$router.push({ path: "/administrator/index" });
                } else if (res.result.userInfo.roleId == 91) {
                  this.$router.push({ path: "/regionalManagers/index" });
                } else if (res.result.userInfo.roleId == 92) {
                  this.$router.push({ path: "/customerManager/index" });
                } else if (res.result.userInfo.roleId == 81) {
                  this.$router.push({ path: "/administrator/index" });
                }
                else if (res.result.userInfo.roleId == 5) {
                  this.$router.push({ path: "/sports/examine" });
                }
                else if (res.result.userInfo.roleId == 2) {
                  this.$router.push({ path: "/course" });
                }else {
                  this.$router.push({ path: "/index" });
                }
                this.$message.success(res.message);
                if (this.checked) {
                  localStorage.setItem(
                    "zhty-accountNumber",
                    encryption(
                      JSON.stringify({
                        ...this.loginForm,
                        checked: this.checked,
                      })
                    )
                  );
                } else {
                  localStorage.removeItem("zhty-accountNumber");
                }
              } else {
                this.$message.error(res.message);
                if (res.code === 500) {
                  this.changeImg();
                }
              }
            })
            .catch(() => {
              this.loading = false;
              this.changeImg();
            });
        } else {
          return false;
        }
      });
    },
  },
};
</script>

<style lang="scss" scoped>
.login-container {
  background-size: 100% 100%;
  -moz-background-size: 100% 100%;
  height: 100%;
  background-image: url("../../assets/login_images/BG.png");
  background-repeat: no-repeat;
  padding: 5% 8%;
  display: flex;
  justify-content: center;
  align-items: center;
  overflow: hidden;
  .login-main {
    width: 1150px;
    background: #ffffff;
    box-shadow: 0px 0px 27px 0px rgba(12, 126, 171, 0.26);
    border-radius: 20px;
    display: flex;

    .main-left {
      .left-top {
        height: 100px;
        display: flex;
        align-items: center;
        padding-left: 30px;
        img {
          width: 80px;
          height: 80px;
        }
        span {
          width: 256px;
          height: 40px;
          line-height: 35px;
          font-size: 36px;
          font-family: Source Han Sans CN;
          font-weight: 400;
          color: #008cff;
          text-shadow: 0px 4px 13px rgba(36, 178, 224, 0.2);
          margin-bottom: 3px;
        }
      }
      .left-main {
        margin-left: 100px;
        margin-top: 30px;
        width: 450px;
        height: 400px;
        background-image: url("../../assets/login_images/loginImg.png");
        background-repeat: no-repeat;
        background-size: 100% 100%;
      }
    }
    .main-right {
      margin-left: 100px;
      height: 600px;
      .login-right-form {
        .login-form {
          margin-top: 50px;
          .welcomeCn {
            line-height: 60px;
            font-size: 36px;
            font-family: Source Han Sans CN;
            font-weight: 400;
            color: #2177ff;
          }
          .form {
            margin-top: 40px;
            ::v-deep .el-form-item__content {
              display: flex;
              justify-content: left;
              .el-input {
                width: 70%;
                border-radius: 4px;
              }
            }
            .pass-box {
              display: flex;
              justify-content: space-between;
              .box-center {
                width: 600px;
                display: flex;
                justify-content: space-between;
              }
            }
            .el-button {
              margin-top: 40px;
              background: linear-gradient(-90deg, #61a6fc, #24ddff);
              width: 311px;
              color: #ffffff;
              border-radius: 10px;
              font-size: 20px;
            }
          }
        }
      }
    }
  }
  .text{
    position: fixed;
    bottom: 2%;
    display: flex;
    justify-content: center;
    font-size: 14px;
    color: #ffffff;
  }
}
::v-deep .el-icon-user:before {
  color: #2277ff;
  font-size: 20px;
}
::v-deep .el-icon-lock:before {
  color: #2277ff;
  font-size: 20px;
}
::v-deep .el-input__inner {
  padding-left: 40px;
  border-top-width: 0px;
  border-left-width: 0px;
  border-right-width: 0px;
  border-bottom-width: 1px;
  /*outline: medium;*/
}
</style>
